package com.fsh.anime.handler;

import com.fsh.anime.model.vos.Result;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;

/**
 * 类名称：$CLASSNAME$
 * <p>
 * 作者：fsh
 * 日期：2023/08/20
 * 描述：全局异常处理程序
 */
@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public Result sqlEx(SQLIntegrityConstraintViolationException e){
        e.printStackTrace();
        if(e.getMessage().contains("Duplicate entry")){
            //开始截取字符串，截到‘zhangsan’
            //按空格截取，装到字符串数组中。那么[2]就是 ‘zhangsan’
            String[] split=e.getMessage().split(" ");
            String msg=split[2]+"已存在，请换一个名字";
            return Result.error(msg);
        }
        return Result.error("系统繁忙，请稍后再试！");
    }
}